package com.example.searchengine.service;

import com.example.searchengine.model.ResultInfo;
import com.example.searchengine.model.Weight;
import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class Index {
    //1.创建正排和倒排索引
    private static  ArrayList<ResultInfo> forwardIndex = new ArrayList<>();
    private static HashMap<String,ArrayList<Weight>> invertIndex = new HashMap<>();

    @PostConstruct
    public static void build() throws IOException {
        //打开文件
        BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("G:/raw_data.txt")));
        //遍历文件
        while (true){
            String line = bufferedReader.readLine();
            if (line == null){
                break;
            }
            //1.1加入正排索引
            ResultInfo resultInfo = buildForward(line);
            //1.2加入倒排索引
            if (resultInfo != null){
                buildInvert(resultInfo);
            }
        }
        System.out.println("---------------");
        System.out.println("索引构建完毕");
    }

    private static void buildInvert(ResultInfo resultInfo) {
        //1.倒排索引是一个 hashMap key为关键词,value 为 ArrayList<Weight>
        //构建一个辅助haspMap
        class AssistWeight{
            public int weight;
            public AssistWeight(int weight){
                this.weight = weight;
            }
        }
        HashMap<String,AssistWeight> hashMap = new HashMap<>();
        //拿到标题,对标题分词
        String title = resultInfo.getTitle();
        List<Term> terms = ToAnalysis.parse(title).getTerms();
        for (Term term:terms) {
            String key = term.getName();
            AssistWeight assistWeight = hashMap.get(key);
            if (assistWeight == null){
                hashMap.put(key,new AssistWeight(10));
            }else {
                assistWeight.weight += 10;
            }
        }
        //拿到内容分词
        String count = resultInfo.getCount();
        List<Term> terms1 = ToAnalysis.parse(count).getTerms();
        for (Term term:terms1) {
            String key = term.getName();
            AssistWeight assistWeight = hashMap.get(key);
            if (assistWeight == null){
                hashMap.put(key,new AssistWeight(1));
            }else {
                assistWeight.weight += 1;
            }
        }
        //将权重计算完毕的分词,加入到倒排索引中
        for (Map.Entry<String,AssistWeight> entry : hashMap.entrySet()) {
            //创建带权重信息的对象
            Weight weight = new Weight();
            weight.setWord(entry.getKey());
            weight.setId(resultInfo.getId());
            weight.setWeight(entry.getValue().weight);
            //加入到倒排索引中
            ArrayList<Weight> list = invertIndex.get(entry.getKey());
            if (list == null){
                list = new ArrayList<>();
                invertIndex.put(entry.getKey(),list);
            }
            list.add(weight);
        }
    }

    private static ResultInfo buildForward(String line) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setId(forwardIndex.size());
        //1.将根据 "\3" 分割成3部分 title url count
        String[] str = line.split("\3");
        if (str.length != 3){
            System.out.println(line + "此行数据不符合");
            return null;
        }
        resultInfo.setTitle(str[0]);
        resultInfo.setUrl(str[1]);
        resultInfo.setCount(str[2]);
        forwardIndex.add(resultInfo);
        return resultInfo;
    }

    //2.创建根据 id 查找内容方法
    public static ResultInfo findId(int id){
        return forwardIndex.get(id);
    }
    //3.创建根据 关键词 查找内容方法
    public static ArrayList<Weight> findKey(String word){
        return invertIndex.get(word);
    }
}
